﻿using Microsoft.IdentityModel.Tokens;

using System.Collections.Generic;

using ZFB.Common;
using ZFB.DTO;
using ZFB.IServices;
using ZFB.Models;
using ZFB.Models.EntityConfig;
using ZFB.Repository;
using ZFB.Repository.City;

namespace ZFB.Services
{

    public class CityServices : ICityServices
    {
        private readonly ICityRepository _cityRepository;
        public CityServices(ICityRepository cityRepository)
        {
            _cityRepository = cityRepository;
        }
        public async Task<ApiResult> AddCityAsync(string CityName)
        {
            if (await _cityRepository.IsExistAsync(e => e.Name == CityName))
            {
                return new ApiResult("城市已存在");
            }
            else
            {
                T_Cities city = new() { Name = CityName };
                var res = await _cityRepository.AddAsync(city);
                return res <= 0 ? new ApiResult("添加失败！") : new ApiResult();
            }
        }
        public async Task<ApiResult> GetCityAsync()
        {
            var list = await _cityRepository.GetListAsync();
            if (!list.IsNullOrEmpty())
                return new ApiResult(list.Select(e => e.Name));
            return new ApiResult("获取失败");
        }

        public async Task<ApiResult> GetCityByIdAsync(long id)
        {
            var data = await _cityRepository.GetListAsync(e => e.Id == id);
            if (data.IsNullOrEmpty())
                return new ApiResult("城市不存在或已删除");
            return new ApiResult(data.Select(e => e.Name));

        }
    }
}